/**
 *
 */
package org.suren.littlebird.views;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;
import org.suren.littlebird.actions.Reload;
import org.suren.littlebird.actions.logger.SuRenLoggerAddBridgeAction;
import org.suren.littlebird.actions.logger.SuRenLoggerBridgeStartAction;
import org.suren.littlebird.actions.logger.SuRenLoggerDelBridgeAction;
import org.suren.littlebird.actions.logger.SuRenLoggerPreferenceAction;
import org.suren.littlebird.actions.logger.SuRenLoggerReload;
import org.suren.littlebird.actions.logger.SuRenLoggerReloadAction;
import org.suren.littlebird.actions.logger.SuRenLoggerSetAllAction;
import org.suren.littlebird.actions.logger.SuRenLoggerSetDebugAction;
import org.suren.littlebird.actions.logger.SuRenLoggerSetErrorAction;
import org.suren.littlebird.actions.logger.SuRenLoggerSetInfoAction;
import org.suren.littlebird.actions.logger.SuRenLoggerSetOffAction;
import org.suren.littlebird.actions.logger.SuRenLoggerSetWarnAction;
import org.suren.littlebird.layout.BorderData;
import org.suren.littlebird.layout.BorderLayout;
import org.suren.littlebird.server.LogServer;
import org.suren.littlebird.views.filter.SuRenLoggerViewerFilter;
import org.suren.littlebird.views.provider.SuRenLoggerFieldProvider;
import org.suren.littlebird.views.provider.SuRenLoggerProvider;

/**
 * @author suren
 * 日志句柄的管理
 */
public class SuRenLoggerView extends ViewPart
{
	private TableViewer	viewer;
	private Table	table;

	@Override
	public void createPartControl(Composite parent)
	{
		parent.setLayout(new BorderLayout());
		final SuRenLoggerViewerFilter filter = new SuRenLoggerViewerFilter();

		viewer = new TableViewer(parent, SWT.MULTI  | SWT.V_SCROLL | SWT.H_SCROLL| SWT.FULL_SELECTION | SWT.BORDER);
		viewer.setContentProvider(new SuRenLoggerProvider());
		viewer.setLabelProvider(new SuRenLoggerFieldProvider());
		viewer.addFilter(filter);
		viewer.addSelectionChangedListener(viewerSelectionChange);

		table = viewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		TableLayout tableColLayout = new TableLayout();
		table.setLayout(tableColLayout);

		tableColLayout.addColumnData(new ColumnWeightData(1, true));
		TableColumn indexCol = new TableColumn(table, SWT.None);
		indexCol.setText("Index");

		tableColLayout.addColumnData(new ColumnWeightData(7, true));
		TableColumn nameCol = new TableColumn(table, SWT.None);
		nameCol.setText("Name");

		tableColLayout.addColumnData(new ColumnWeightData(1, true));
		TableColumn levelCol = new TableColumn(table, SWT.None);
		levelCol.setText("Level");

		tableColLayout.addColumnData(new ColumnWeightData(1, true));
		TableColumn bridgesCol = new TableColumn(table, SWT.None);
		bridgesCol.setText("Bridges");

		createActions();

		createToolBar();

		createContextaMenu();

		final Text text = new Text(parent, SWT.BORDER);
		text.setLayoutData(BorderData.NORTH);
		text.setToolTipText("Type The Filter Text");
		text.setMessage("Filter");
		text.addListener(SWT.CHANGED, new Listener()
		{

			@Override
			public void handleEvent(Event event)
			{
				filter.setName(text.getText());
				viewer.refresh();
			}
		});
	}

	private Action setAllAction;
	private Action setDebugAction;
	private Action setInfoAction;
	private Action setWarnAction;
	private Action setErrorAction;
	private Action setOffAction;
	private Action reloadAction;
	private Action startAction;
	private Action addBridgeAction;
	private Action delBridgeAction;
	private LogServer logServer;

	/**
	 *
	 */
	private void createActions()
	{
		logServer = new LogServer();
		IStatusLineManager iStatusLineManager = getViewSite().getActionBars().getStatusLineManager();

		Reload reload = new SuRenLoggerReload(viewer, iStatusLineManager);

		setAllAction = new SuRenLoggerSetAllAction(table, reload);
		setDebugAction = new SuRenLoggerSetDebugAction(table, reload);
		setInfoAction = new SuRenLoggerSetInfoAction(table, reload);
		setWarnAction = new SuRenLoggerSetWarnAction(table, reload);
		setErrorAction = new SuRenLoggerSetErrorAction(table, reload);
		setOffAction = new SuRenLoggerSetOffAction(table, reload);

		addBridgeAction = new SuRenLoggerAddBridgeAction(table, reload);
		delBridgeAction = new SuRenLoggerDelBridgeAction(table, reload);

		reloadAction = new SuRenLoggerReloadAction(reload);
		startAction = new SuRenLoggerBridgeStartAction(logServer);

		setAllAction.setEnabled(false);
		setDebugAction.setEnabled(false);
		setInfoAction.setEnabled(false);
		setWarnAction.setEnabled(false);
		setErrorAction.setEnabled(false);
		setOffAction.setEnabled(false);
		addBridgeAction.setEnabled(false);
		delBridgeAction.setEnabled(false);
	}

	/**
	 *
	 */
	private void createToolBar()
	{
		IActionBars actionBars = getViewSite().getActionBars();

		IToolBarManager toolBarMgr = actionBars.getToolBarManager();
		toolBarMgr.add(setAllAction);
		toolBarMgr.add(setDebugAction);
		toolBarMgr.add(setInfoAction);
		toolBarMgr.add(setWarnAction);
		toolBarMgr.add(setErrorAction);
		toolBarMgr.add(setOffAction);
		toolBarMgr.add(new Separator());
		toolBarMgr.add(addBridgeAction);
		toolBarMgr.add(delBridgeAction);
		toolBarMgr.add(new Separator());
		toolBarMgr.add(reloadAction);
		toolBarMgr.add(startAction);

		IMenuManager menuMgr = actionBars.getMenuManager();
		menuMgr.add(new SuRenLoggerPreferenceAction(getViewSite().getShell()));
	}

	private void createContextaMenu()
	{
//		MenuManager menuMgr = new MenuManager();
//		menuMgr.setRemoveAllWhenShown(true);

//		menuMgr.add(setAllAction);
//		menuMgr.add(reloadAction);
//		menuMgr.add(startAction);
//
//		Menu menu = menuMgr.createContextMenu(viewer.getControl());
//		viewer.getTable().setMenu(menu);
//		getSite().registerContextMenu("test", menuMgr, viewer);
	}

	@Override
	public void setFocus()
	{
		viewer.getControl().setFocus();
	}

	private ISelectionChangedListener viewerSelectionChange = new ISelectionChangedListener()
	{

		@Override
		public void selectionChanged(SelectionChangedEvent event)
		{
			boolean  enabled = !event.getSelection().isEmpty();

			setAllAction.setEnabled(enabled);
			setDebugAction.setEnabled(enabled);
			setInfoAction.setEnabled(enabled);
			setWarnAction.setEnabled(enabled);
			setErrorAction.setEnabled(enabled);
			setOffAction.setEnabled(enabled);
			addBridgeAction.setEnabled(enabled);
			delBridgeAction.setEnabled(enabled);
		}
	};
}
